<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceNumber">
        <el-input
          v-model="queryParams.deviceNumber"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specifications">
        <el-input
          v-model="queryParams.specifications"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制造厂家" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入制造厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备期数" prop="devicePeriod">
        <el-input
          v-model="queryParams.devicePeriod"
          placeholder="请输入设备期数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出厂编号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入出厂编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工段" prop="workshop">
        <el-input
          v-model="queryParams.workshop"
          placeholder="请输入工段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序号" prop="operationNumber">
        <el-input
          v-model="queryParams.operationNumber"
          placeholder="请输入工序号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="operationName">
        <el-input
          v-model="queryParams.operationName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="加工零件" prop="manufacturedParts">
        <el-input
          v-model="queryParams.manufacturedParts"
          placeholder="请输入加工零件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际分布区域" prop="workAreas">
        <el-input
          v-model="queryParams.workAreas"
          placeholder="请输入实际分布区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['gelly:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gelly:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gelly:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gelly:device:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportQrCode"
          v-hasPermi="['gelly:device:export']"
        >导出设备二维码</el-button>
      </el-col>
      <el-col span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-full-screen"
          size="mini"
          @click="handleScanner"
          v-hasPermi="['gelly:device:export']"
        >扫描识别设备</el-button>
      </el-col>
      <el-col span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-data-line"
          size="mini"
          @click="viewAllWaveformHistory"
          v-hasPermi="['gelly:device:list']"
        >设备波形示教</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="设备编号" align="center" prop="deviceNumber" width="180">
        <template slot-scope="scope">
          <router-link type="primary" :to="'/gelly/device-fault/index/'+scope.row.deviceNumber" class="link-type">
            <span>{{scope.row.deviceNumber}}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="规格型号" align="center" prop="specifications" />
      <el-table-column label="制造厂家" align="center" prop="manufacturer" />
      <el-table-column label="设备期数" align="center" prop="devicePeriod" />
      <el-table-column label="出厂编号" align="center" prop="serialNumber" />
      <el-table-column label="工段" align="center" prop="workshop" />
      <el-table-column label="工序号" align="center" prop="operationNumber" />
      <el-table-column label="工序名称" align="center" prop="operationName" />
      <el-table-column label="加工零件" align="center" prop="manufacturedParts" />
      <el-table-column label="实际分布区域" align="center" prop="workAreas" />
      <el-table-column
        label="操作"
        align="center"
        width="280"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gelly:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gelly:device:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-data"
            @click="handleWaveform(scope.row)"
          >波形示教</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceNumber">
          <el-input v-model="form.deviceNumber" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specifications">
          <el-input v-model="form.specifications" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="制造厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入制造厂家" />
        </el-form-item>
        <el-form-item label="设备期数" prop="devicePeriod">
          <el-input v-model="form.devicePeriod" placeholder="请输入设备期数" />
        </el-form-item>
        <el-form-item label="出厂编号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入出厂编号" />
        </el-form-item>
        <el-form-item label="工段" prop="workshop">
          <el-input v-model="form.workshop" placeholder="请输入工段" />
        </el-form-item>
        <el-form-item label="工序号" prop="operationNumber">
          <el-input v-model="form.operationNumber" placeholder="请输入工序号" />
        </el-form-item>
        <el-form-item label="工序名称" prop="operationName">
          <el-input v-model="form.operationName" placeholder="请输入工序名称" />
        </el-form-item>
        <el-form-item label="加工零件" prop="manufacturedParts">
          <el-input v-model="form.manufacturedParts" placeholder="请输入加工零件" />
        </el-form-item>
        <el-form-item label="实际分布区域" prop="workAreas">
          <el-input v-model="form.workAreas" placeholder="请输入实际分布区域" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/gelly/device";
import {download} from "@/utils/request";

export default {
  name: "Device",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceNumber: null,
        deviceName: null,
        specifications: null,
        manufacturer: null,
        devicePeriod: null,
        serialNumber: null,
        workshop: null,
        operationNumber: null,
        operationName: null,
        manufacturedParts: null,
        workAreas: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      qrData:''
    };
  },
  created() {
    this.getList();
    // 从路由参数中获取扫描结果
    const query = this.$router.currentRoute.value.query;
    if (query.qrData) {
      this.qrData = decodeURIComponent(query.qrData);
    }
    console.log(query.qrData);
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        deviceNumber: null,
        deviceName: null,
        specifications: null,
        manufacturer: null,
        devicePeriod: null,
        serialNumber: null,
        workshop: null,
        operationNumber: null,
        operationName: null,
        manufacturedParts: null,
        workAreas: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getDevice(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceId != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 新增时将设备编号赋值给设备ID
            this.form.deviceId = this.form.deviceNumber;
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + deviceIds + '"的数据项？').then(function() {
        return delDevice(deviceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gelly/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)

    },
    /** 导出二维码 */
    handleExportQrCode() {
      download('gelly/device/exportQrCode', {
        ...this.queryParams
      }, `QrCode_${new Date().getTime()}.zip`)
    },
    handleScanner(){
      this.$router.push({name:'QrCodeScanner'});
      // this.$router.push({path:'/gelly/device-fault/index/'+591})
    },
    viewAllWaveformHistory() {
      // 导航到所有设备波形示教页面（修复路径）
      this.$router.push({ path: `/real/waveform-index/history` });
    },
    handleWaveform(row) {
      this.$router.push({
        path: '/real/waveform-index/history',
        query: {
          deviceId: row.deviceNumber
        }
      })
    }
  }
};
</script>
